package com.hzqy.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.system.SysConfigDao;

/**
 * 
 * 容器初始化(spring bean都加载完成后) 执行初始化操作：
 *    1. 加载sys_config表的配置
 * 
 * @author Administrator
 *
 */
@Component
public class SystemConfigInitialization implements ApplicationListener<ContextRefreshedEvent> {

	private final static Logger LOG = LoggerFactory.getLogger(SystemConfigInitialization.class);

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    	ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
    	if(applicationContext.getParent() == null) {
    		initSystemConfig(contextRefreshedEvent.getApplicationContext());
    	}
		LOG.debug("SystemConfigInitialization--初始化从数据库表sys_config中读取系统配置完成。");
    }
	
	private void initSystemConfig(ApplicationContext applicationContext) {
		SysConfigDao sysConfigDao = (SysConfigDao) applicationContext.getBean("sysConfigDao"); //获得springmvc注入的对象
		boolean flag = SystemConfigUtils.init(sysConfigDao);
		LOG.debug("SystemConfigInitialization--读取sys_config数据库表配置状态："+flag);
	}
	
}